<?php
	class Thekhuyenmai_model extends CI_Model 
	{
    	function LayTatCaTheKhuyenMai() 
		{
    	    $post_per_page = 20;

        	$this->db->select('*');
	        $this->db->from('TheKhuyenMai');
    	    $this->db->order_by('NgayKichHoat', 'desc');
        	$this->db->limit($post_per_page, $this->uri->segment(3));
	        $query = $this->db->get();

    	    $this->load->library('pagination');
        	$config['base_url'] = base_url() . 'index.php/thekhuyenmai/admin_index';
	        $config['total_rows'] = $this->db->get('TheKhuyenMai')->num_rows();
    	    $config['per_page'] = $post_per_page;
        	$this->pagination->initialize($config);

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;
	    }

    	function Tim($key) 
		{
    	    $post_per_page = 20;
        	$num = 0;
	        if ($this->uri->segment(4))
    	        $num = $this->uri->segment(4);

	        $this->db->select('*');
    	    $this->db->from('TheKhuyenMai');
        	$this->db->where('SoThe', $key);
	        $this->db->order_by('NgayKichHoat', 'desc');
    	    $this->db->limit($post_per_page, $num);
        	$query = $this->db->get();

	        $this->db->select('*');
    	    $this->db->from('TheKhuyenMai');
        	$this->db->where('SoThe', $key);
	        $total_query = $this->db->get();

    	    $this->load->library('pagination');
	        $config['uri_segment'] = 4;
    	    $config['base_url'] = base_url() . 'index.php/thekhuyenmai/Tim/' . $key;
        	$config['total_rows'] = $total_query->num_rows();
	        $config['per_page'] = $post_per_page;
    	    $this->pagination->initialize($config);

        	if ($query->num_rows() > 0) 
			{
    	        foreach ($query->result() as $row) 
				{
            	    $data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}
		
		function LaySoThe_ajax($key)
		{
			$this->db->select('SoThe AS Ten');
			$this->db->from('TheKhuyenMai');
			$this->db->like('SoThe', $key);
			$this->db->order_by('SoThe', "asc");
			$query = $this->db->get();
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}

	    function Reset() 
		{
	        $strSQL = "UPDATE TheKhuyenMai SET PhanTramGiam = 2, Diem = 0
						WHERE (MONTH(NgayKichHoat) = MONTH(NOW()) AND DAY(NgayKichHoat) = DAY(NOW())) 
						OR (MONTH(NgayKichHoat) = 2 AND DAY(NgayKichHoat) = 29 AND MONTH(NOW()) = 2 AND DAY(NOW()) = 28)";
    	    return $this->db->query($strSQL);
	    }

    	function Xoa($sothe) 
		{
    	    return $this->db->delete('TheKhuyenMai', array('SoThe' => $sothe));
	    }

	    function Them($sothe) 
		{
        	$data = array(
			            	'SoThe' => $sothe,
	            			'PhanTramGiam' => 2,
    				        'Diem' => 0,
				            'NgayKichHoat' => date('Y-m-d', strtotime("now"))
				        );

	        $this->db->insert('TheKhuyenMai', $data);
    	    return $this->db->affected_rows();
	    }

    	function get_records_the_i12() 
		{
    	    $sothe = $this->input->post('sothe');
			if($sothe != 0){
				$this->db->select('SoThe, Diem, PhanTramGiam');
				$this->db->where('SoThe', $sothe);
				$q = $this->db->get('TheKhuyenMai');
			
				if ($q->num_rows() > 0) 
				{
					foreach ($q->result() as $row) 
					{
						$data[0] = $row;
						$data[1] = 1;
					}
					return $data;
				}
				$data[0] = 0;
				$data[1] = 0;
				return $data;
			}
			$data[0] = 0;
    	    $data[1] = -1;
        	return $data;
	    }
		
		function update_records_the_i12($tongtien) 
		{
    	    $sothe = $this->input->post('sothe');
	        $this->db->select('SoThe, Diem, PhanTramGiam');
    	    $this->db->where('SoThe', $sothe);
	        $q = $this->db->get('TheKhuyenMai');

    	    if ($q->num_rows() > 0) 
			{
    	        foreach ($q->result() as $row) 
				{
	                $sothe = $row->SoThe;
					$diem = $row->Diem;
					$phantramgiam = $row->PhanTramGiam;
        	    }
    	    }
			$diemcong = floor($tongtien/1000000);
			$diem = $diem + $diemcong;
	        $phantramcong = floor($diem/10);
			$diem = $diem - $phantramcong*10;
			$phantramgiam = $phantramgiam + $phantramcong;
			$this->db->where('SoThe', $sothe);
            $this->db->set('PhanTramGiam', $phantramgiam);
			$this->db->set('Diem', $diem);
            $this->db->update('TheKhuyenMai');			
	    }
	}
?>